#include "head.h"

int workCmd(int netFd){
    //初始化名字
    printf("Plz enter user name:");
    char name[50];
    scanf("%s",name);

    train_t train;
    train.length = strlen(name);
    memcpy(train.data,name,train.length);
    send(netFd,&train.length,sizeof(train.length),MSG_NOSIGNAL);
    send(netFd,train.data,train.length,MSG_NOSIGNAL);

    //验证密码
    while(1){
        //发送密码
        printf("Plz input passwd:");
        char password[256];
        scanf("%s",password);
        bzero(&train,sizeof(train));
        train.length = strlen(password);
        memcpy(train.data,password,train.length);
        send(netFd,&train.length,sizeof(train.length),MSG_NOSIGNAL);
        send(netFd,train.data,train.length,MSG_NOSIGNAL);

        //接收返回值
        int dataLength = 0;
        bzero(password,sizeof(password));
        recvn(netFd,&dataLength,sizeof(int));
        recvn(netFd,password,dataLength);
        /* printf("Passwd is %s\n",password); */
        if(strcmp("OK",password)==0){
            printf("Passwd is right\n");
            break;
        }
        else{
            printf("Passwd is wrong\n");
        }
    }
    client_t *client = (client_t*)calloc(1,sizeof(client_t));
    client->fd = netFd;
    strcpy(client->userName,name);
    //接收第一创建用户文件夹和变换路径回执
    int dirFirstLength = 0;
    char dirCheck[10] = {0};
    recvn(netFd,&dirFirstLength,sizeof(int));
    recvn(netFd,dirCheck,dirFirstLength);

    //初始地址什么都没
    strcpy(client->pwd,client->userName);
    while(1){
        printf("%s$ ",client->pwd);
        fflush(stdin);
        fflush(stdout);

        //获取命令
        char temp[1024] = {0};
        read(STDIN_FILENO,temp,sizeof(temp));
        temp[strlen(temp)-1] = 0;

        //分离命令
        char cmd [1024] = {0};
        strcpy(cmd,temp);
        char cmdfix[1024] = {0};//存放处理好的数据
        char *p = strtok(temp," ");
        strcpy(cmd,p);//cmd存放系统调用命令
        int first = 1;
        while(p != NULL){
            if(first){
                first = 0;
            }else{
                strcat(cmdfix,p);
            }
            p = strtok(NULL," ");
        }

        if(strncmp("cd",cmd,2)==0){
            TLVtrain_t tlv;
            bzero(&tlv,sizeof(tlv));
            tlv.type = CD ;
            strcpy(tlv.data,cmdfix);
            printf("%s\n",tlv.data);
            tlv.length = strlen(cmdfix);
            send(netFd,&tlv.type,sizeof(tlv.type),MSG_NOSIGNAL);
            send(netFd,&tlv.length,sizeof(tlv.length),MSG_NOSIGNAL);
            send(netFd,tlv.data,tlv.length,MSG_NOSIGNAL);

            // *** THIS IS THE KEY CHANGE ***
            // 1. Receive status code first
            int status = 0;
            recvn(netFd, &status, sizeof(status));

            // 2. Receive the data payload (either path or error message)
            bzero(&train, sizeof(train));
            recvn(netFd, &train.length, sizeof(train.length));
            recvn(netFd, train.data, train.length);
            train.data[train.length] = '\0'; // Ensure null-termination

            // 3. Act based on the status code
            if (status == 0) { // Success
                strcpy(client->pwd, train.data);
                printf("Cd successful.\n");
                printf("New path: %s\n", client->pwd);
            } else { // Failure
                printf("Error: %s\n", train.data);
            }
        }
        else if(strcmp("ls",cmd)==0){
            TLVtrain_t tlv;
            bzero(&tlv,sizeof(tlv));
            tlv.type = LS ;
            strcpy(tlv.data,"Y");
            tlv.length = strlen(tlv.data);
            send(netFd,&tlv.type,sizeof(tlv.type),MSG_NOSIGNAL);
            send(netFd,&tlv.length,sizeof(tlv.length),MSG_NOSIGNAL);
            send(netFd,tlv.data,tlv.length,MSG_NOSIGNAL);
            /* printf("%d %d %s\n",tlv.type,tlv.length,tlv.data); */

            char dirName [1024] = {0};
            int dirLength = 0;
            while(1){
                bzero(dirName,sizeof(dirName));
                recvn(netFd,&dirLength,sizeof(int));
                recvn(netFd,dirName,dirLength);
                if(dirLength == 0){
                    break;
                }
                printf("%s ",dirName);
            }
            printf("\n");
        }
        else if(strcmp("ll",cmd)==0){
            TLVtrain_t tlv;
            bzero(&tlv,sizeof(tlv));
            tlv.type = LL ;
            tlv.length = 1;
            strcpy(tlv.data,"Y");
            send(netFd,&tlv.type,sizeof(tlv.type),MSG_NOSIGNAL);
            send(netFd,&tlv.length,sizeof(tlv.length),MSG_NOSIGNAL);
            send(netFd,tlv.data,tlv.length,MSG_NOSIGNAL);

            char dirInfo [1024] = {0};
            int infoLength = 0;
            while(1){
                bzero(dirInfo,sizeof(dirInfo));
                recvn(netFd,&infoLength,sizeof(int));
                recvn(netFd,dirInfo,infoLength);
                if(infoLength == 0){
                    break;
                }
                printf("%s\n",dirInfo);
            }
            printf("\n");
        }
        else if (strcmp("pwd",cmd)==0){
            printf("%s\n",client->pwd);
        }
        else if(strncmp("puts",cmd,4)==0){
            if(access(cmdfix,F_OK) == -1){
                printf("Local file does not exist!\n");
            }else{
                TLVtrain_t tlv;
                bzero(&tlv,sizeof(tlv));
                tlv.type = PUTS ;
                strcpy(tlv.data,cmdfix);
                tlv.length = strlen(cmdfix);
                send(netFd,&tlv.type,sizeof(tlv.type),MSG_NOSIGNAL);
                send(netFd,&tlv.length,sizeof(tlv.length),MSG_NOSIGNAL);
                send(netFd,tlv.data,tlv.length,MSG_NOSIGNAL);
                printf("%d %d %s\n",tlv.type,tlv.length,tlv.data);
                //构建路径
                char fullPath[1024] = {0};
                strcat(fullPath,"./");
                strcat(fullPath,cmdfix);
                //发送文件
                printf("Send.\n");
                sendFile_puts(netFd,cmdfix);
            }
        }
        else if(strncmp("gets",cmd,4)==0){
            TLVtrain_t tlv;
            bzero(&tlv,sizeof(tlv));
            tlv.type = GETS ;
            strcpy(tlv.data,cmdfix);
            tlv.length = strlen(cmdfix);
            send(netFd,&tlv.type,sizeof(tlv.type),MSG_NOSIGNAL);
            send(netFd,&tlv.length,sizeof(tlv.length),MSG_NOSIGNAL);
            send(netFd,tlv.data,tlv.length,MSG_NOSIGNAL);
            //回执，看是否有这个文件，没有就提示
            bzero(&train,sizeof(train));
            recvn(netFd,&train.length,sizeof(train.length));
            recvn(netFd,train.data,train.length);
            if(strcmp(train.data,"File not exists")==0){
                printf("%s.\n",train.data);
            }
            else{
                recvFile_gets(netFd,NULL);
            }
        }
        else if(strncmp("remove",cmd,6)==0){
            TLVtrain_t tlv;
            bzero(&tlv,sizeof(tlv));
            tlv.type = REMOVE ;
            strcpy(tlv.data,cmdfix);
            tlv.length = strlen(cmdfix);
            send(netFd,&tlv.type,sizeof(tlv.type),MSG_NOSIGNAL);
            send(netFd,&tlv.length,sizeof(tlv.length),MSG_NOSIGNAL);
            send(netFd,tlv.data,tlv.length,MSG_NOSIGNAL);
            //回执
            bzero(&train,sizeof(train));
            recvn(netFd,&train.length,sizeof(train.length));
            recvn(netFd,train.data,train.length);
            if(strcmp(train.data,"Y")==0){
                printf("File deleted successfully.\n");
            }else{
                printf("File deleted error.\n");                                                                                    
            }
        }
        else if(strncmp("mkdir",cmd,5)==0){
            TLVtrain_t tlv;
            bzero(&tlv,sizeof(tlv));
            tlv.type = MKDIR ;
            strcpy(tlv.data,cmdfix);
            tlv.length = strlen(cmdfix);
            send(netFd,&tlv.type,sizeof(tlv.type),MSG_NOSIGNAL);
            send(netFd,&tlv.length,sizeof(tlv.length),MSG_NOSIGNAL);
            send(netFd,tlv.data,tlv.length,MSG_NOSIGNAL);
            //回执
            bzero(&train,sizeof(train));
            recvn(netFd,&train.length,sizeof(train.length));
            recvn(netFd,train.data,train.length);
            if(strcmp(train.data,"Y")==0){
                printf("Dir create successfully.\n");
            }else{
                printf("Dir create error.\n");                                                                                    
            }
        }
        else if(strncmp("rmdir",cmd,5)==0){
            TLVtrain_t tlv;
            bzero(&tlv,sizeof(tlv));
            tlv.type = RMDIR ;
            strcpy(tlv.data,cmdfix);
            tlv.length = strlen(cmdfix);
            send(netFd,&tlv.type,sizeof(tlv.type),MSG_NOSIGNAL);
            send(netFd,&tlv.length,sizeof(tlv.length),MSG_NOSIGNAL);
            send(netFd,tlv.data,tlv.length,MSG_NOSIGNAL);
            //回执
            bzero(&train,sizeof(train));
            recvn(netFd,&train.length,sizeof(train.length));
            recvn(netFd,train.data,train.length);
            if(strcmp(train.data,"Y")==0){
                printf("Dir deleted successfully.\n");
            }else{
                printf("Dir deleted error.\n");                                                                                    
            }
        }
        else if(strcmp("quit",cmd)==0){
            TLVtrain_t tlv;
            bzero(&tlv,sizeof(tlv));
            tlv.type = QUIT ;
            tlv.length = 1;
            strcpy(tlv.data,"N");
            send(netFd,&tlv.type,sizeof(tlv.type),MSG_NOSIGNAL);
            send(netFd,&tlv.length,sizeof(tlv.length),MSG_NOSIGNAL);
            send(netFd,tlv.data,tlv.length,MSG_NOSIGNAL);
            printf("Bye-bye!\n");
            break;
        }
        else{
            //命令输入错误
            printf("Plz input right cmd or 'quit'.\n");
        }
    }
    return 0;
}


